package nyist.media.controller;

import nyist.media.annotation.Log;
import nyist.media.bean.auth.MenuAuthListBean;
import nyist.media.entity.admin.MenuAuth;
import nyist.media.page.ApiResult;
import nyist.media.page.AppPage;
import nyist.media.service.MenuAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 菜单权限
 *
 * @author dcf
 */
@RestController
@RequestMapping(value = "/admin/menu_auths", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MenuAuthController extends BaseController {

    @Autowired
    private MenuAuthService menuAuthService;

    /**
     * 分页查询权限信息
     *
     * @param page     页码
     * @param limit    页长
     * @param parentId 父类菜单ID
     * @return 权限列表
     */
    @GetMapping
    public Map<String, Object> list(Integer page, Integer limit, Long parentId) {
        MenuAuth parent = menuAuthService.find(parentId);
        AppPage<MenuAuthListBean> appPage = menuAuthService.findPage(parentId, new PageRequest(page - 1, limit, Sort.Direction.ASC, MenuAuth.ORDER_PROPERTY_NAME));
        Map<String, Object> map = toResultPage(appPage);
        if (parent != null && parent.getParent() != null) {
            map.put("grandParentId", parent.getParent().getId());
        }
        return map;
    }

    /**
     * 查询菜单权限信息
     *
     * @param id 权限ID
     * @return 权限详细信息
     */
    @GetMapping("{id:\\d+}")
    public Map<String, Object> view(@PathVariable Long id) {
        return ApiResult.SUCCESS.getMap().add("result", menuAuthService.getInfo(id));
    }

    @Log("更新菜单权限")
    @PutMapping("{id:\\d+}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody MenuAuthListBean param) {
        param.setId(id);
        menuAuthService.update(param);
        return ApiResult.SUCCESS.getMap();
    }

    @Log("新增菜单权限")
    @PostMapping
    public Map<String, Object> save(MenuAuthListBean param) {
        menuAuthService.save(param);
        return ApiResult.SUCCESS.getMap();
    }

    @Log("删除菜单权限")
    @DeleteMapping("{id:\\d+}")
    public Map<String, Object> delete(@PathVariable Long id) {
        try {
            menuAuthService.delete(id);
            return ApiResult.SUCCESS.getMap();
        } catch (DataIntegrityViolationException e) {
            return ApiResult.SUCCESS.getMap("数据已经关联, 不能删除");
        }
    }

    @Log(value = "切换菜单状态")
    @PutMapping("/{id}/toggle_enabled")
    public Map<String, Object> switchStatus(@PathVariable Long id) {
        return ApiResult.SUCCESS.getMap().add("enabled", menuAuthService.switchStatus(id).isEnabled());
    }

    @Log(value = "是否首页显示")
    @PutMapping("/{id}/show")
    public Map<String, Object> isShow(@PathVariable Long id) {
        return ApiResult.SUCCESS.getMap().add("isShow", menuAuthService.show(id).isShowMenu());
    }


}
